package com.wt.struggle.goodbye.main.activity

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.os.Message
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import kotlinx.android.synthetic.main.search_layout.*
import kotlinx.android.synthetic.main.text_layout.view.*

/**
 * 搜索界面
 */
class SearchActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {
            Send.GET_SEARCH_HOT_CODE -> {
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    val list: ArrayList<NewsInfo> =
                        gson!!.fromJson(result, object : TypeToken<ArrayList<NewsInfo>>() {}.type)
                    initHot(list)
                }

            }

            Send.GET_SEARCH_HISTORY_CODE -> {
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    val list: ArrayList<NewsInfo> =
                        gson!!.fromJson(result, object : TypeToken<ArrayList<NewsInfo>>() {}.type)
                    initHistory(list)
                }
            }

            Send.SEARCH_LIST -> {

                removeLoadDialog()

                if (netInfo.code == Config.SUCCESS) {

                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val intent = Intent(this@SearchActivity, SearchListActivity::class.java)
                        intent.putExtra("data", result)
                        startActivity(intent)
                    } else {
                        showToastShort("暂无数据")
                    }
                }
            }

            /// 历史记录删除返回
            Send.DELETE_TIE -> {

                removeLoadDialog()
                if (netInfo.code == Config.SUCCESS) {
                    historyFlexBox.removeAllViews()
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.search_layout)

        imageViewBack.setOnClickListener {
            finish()
        }

        initClick()

        getSearch(1)
        getSearch(2)

    }

    var page = 1

    /**
     * 获取热门搜索
     */
    private fun getSearch(code: Int) {
        send!!.getSearch(code, page, handler)
    }

    private fun initHot(list: ArrayList<NewsInfo>) {

        hotFlexBox.removeAllViews()

        for (temp in list) {
            val hotView = layoutInflater.inflate(R.layout.text_layout, null)
            val textView = hotView.textView
            textView.text = temp.keywords

            hotView.setOnClickListener {
                send!!.getSearchList(1, 10, 0, 0, 0, temp.keywords!!, null, handler!!)
                showLoadDialog("搜索中")
            }

            hotFlexBox.addView(hotView)
        }
    }

    private fun initHistory(list: ArrayList<NewsInfo>) {
        historyFlexBox.removeAllViews()

        for (temp in list) {
            val historyView = layoutInflater.inflate(R.layout.text_layout, null)
            val textView = historyView.textView
            textView.text = temp.keywords

            historyView.setOnClickListener {
                send!!.getSearchList(1, 10, 0, 0, 0, temp.keywords!!, null, handler!!)
            }

            historyFlexBox.addView(historyView)
        }

    }

    private fun initClick() {

        searchEditText.setOnEditorActionListener { p0, p1, p2 ->

            send!!.getSearchList(1, 10, 0, 0, 0, searchEditText.text.toString(), null, handler!!)
            showLoadDialog("搜索中")
            false
        }


        imageViewClearHistory.setOnClickListener {
            deleteHistorySearch()
        }


        tvCancel.setOnClickListener {
            send!!.getSearchList(1, 10, 0, 0, 0, searchEditText.text.toString(), null, handler!!)
            showLoadDialog("搜索中")
        }
    }


    /**
     * 删除历史搜索
     */
    private fun deleteHistorySearch() {
        val deleteDialog = AlertDialog.Builder(this)
        deleteDialog.setTitle("提示").setMessage("是否删除历史记录?")
            .setPositiveButton("删除") { dialog, _ ->

                send!!.deleteHistory(handler)
                showLoadDialog("删除中")
                dialog.dismiss()

            }.setNegativeButton("取消") { _, _ ->


            }.show()


    }
}